<template>
  <div class="wrapper">
    <div>
      <el-row :gutter="10" style="z-index:200;margin-top:10px">
        <el-col :xs="22" :lg="22" :offset="2">
          <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item :to="{ path: '/PersonalCenter' }">个人中心</el-breadcrumb-item>
            <el-breadcrumb-item>个人资料</el-breadcrumb-item>
          </el-breadcrumb>
        </el-col>
      </el-row>
      <el-row :gutter="10" style="z-index:200;background:white;margin-bottom:80px">
        <el-col :xs="22" :lg="22" :offset="2" style="margin-top:60px;margin-bottom:30px">
          <span class="personal_info">个人资料</span>
          <span style="margin-left:40px">{{coltitle}}</span>
        </el-col>
        <el-col :xs="22" :lg="22" :offset="2">
          <el-tabs
            :tab-position="tabPosition"
            style="height: 600px;background:white"
            @tab-click="handleClick"
          >
            <el-tab-pane label="基础资料">
              <el-form ref="form" :model="form" label-width="80px" :rules="rules" class="ruleForm">
                <el-form-item label="昵称" prop="name">
                  <el-input v-model="form.name" style="width:400px"></el-input>
                </el-form-item>
                <el-form-item label="性别" prop="sex">
                  <el-radio v-model="form.radio" label="1">男</el-radio>
                  <el-radio v-model="form.radio" label="2">女</el-radio>
                </el-form-item>
                <el-form-item label="学历" prop="degree">
                  <el-select v-model="form.degree.selected" placeholder="请选择学历" style="width:400px">
                    <el-option
                      v-for="item in form.degree.options"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="行业" prop="major">
                  <el-select v-model="form.major.selected" placeholder="请选择行业" style="width:400px">
                    <el-option
                      v-for="item in form.major.options"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" @click="submitForm('form',form)">保存</el-button>
                  <!-- <el-button @click="resetForm('ruleForm2')">重置</el-button> -->
                </el-form-item>
              </el-form>
            </el-tab-pane>
            <el-tab-pane label="头像设置">
              <div class="upload">
                <el-upload
                  class="avatar-uploader"
                  :action="url"
                  :show-file-list="false"
                  :on-success="handleAvatarSuccess"
                  :before-upload="beforeAvatarUpload"
                  :headers="headers"
                >
                  <img v-if="imageUrl" :src="imageUrl" class="avatar">
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                  <span class="tip">上传头像</span>
                </el-upload>
              </div>
            </el-tab-pane>
            <el-tab-pane label="账号信息">
              <el-form label-width="80px" class="ruleForm" v-if="!form.edit">
                <el-form-item label="密码" prop="pwd">
                  <span
                    style="width:150px;display:inline-block"
                  >{{form.pwd.hide?form.pwd.hidevalue:form.pwd.value}}</span>
                  <span
                    class="iconfont seefont"
                    @click="form.pwd.hide=!form.pwd.hide"
                    v-show="form.pwd.hide"
                  >&#xe796;</span>
                  <span
                    class="iconfont seefont"
                    @click="form.pwd.hide=!form.pwd.hide"
                    v-show="!form.pwd.hide"
                  >&#xe786;</span>
                  <span class="editfont" @click="dialogVisible=true">编辑</span>
                </el-form-item>
                <el-form-item label="邮箱" prop="email">
                  <span
                    style="width:150px;display:inline-block"
                  >{{form.email.hide?form.email.hidevalue:form.email.value}}</span>
                  <span
                    class="iconfont seefont"
                    @click="form.email.hide=!form.email.hide"
                    v-show="form.email.hide"
                  >&#xe796;</span>
                  <span
                    class="iconfont seefont"
                    @click="form.email.hide=!form.email.hide"
                    v-show="!form.email.hide"
                  >&#xe786;</span>
                  <span class="editfont" @click="emailVisible=true">编辑</span>
                </el-form-item>
              </el-form>
            </el-tab-pane>
            <el-dialog
              title="提示"
              :visible.sync="dialogVisible"
              width="30%"
              :modal="false"
              v-if="dialogVisible"
            >
              <div>
                <el-form
                  :model="ruleForm2"
                  status-icon
                  :rules="rules2"
                  ref="ruleForm2"
                  label-width="100px"
                  class="demo-ruleForm"
                >
                  <el-form-item label="原密码" prop="prepass">
                    <el-input type="password" v-model="ruleForm2.prepass" autocomplete="off"></el-input>
                  </el-form-item>
                  <el-form-item label="新密码" prop="pass">
                    <el-input type="password" v-model="ruleForm2.pass" autocomplete="off"></el-input>
                  </el-form-item>
                  <el-form-item label="确认密码" prop="checkPass">
                    <el-input type="password" v-model="ruleForm2.checkPass" autocomplete="off"></el-input>
                  </el-form-item>
                </el-form>
              </div>
              <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="handleConfirm('ruleForm2',ruleForm2)">确 定</el-button>
              </span>
            </el-dialog>
            <el-dialog
              title="提示"
              :visible.sync="emailVisible"
              width="30%"
              :modal="false"
              v-if="emailVisible"
            >
              <div>
                <el-form
                  :model="ruleForm2"
                  status-icon
                  :rules="rules2"
                  ref="ruleForm3"
                  label-width="100px"
                  class="demo-ruleForm"
                >
                  <el-form-item label="新邮箱" prop="pass">
                    <el-input type="email" v-model="form.email.value" autocomplete="off"></el-input>
                  </el-form-item>
                </el-form>
              </div>
              <span slot="footer" class="dialog-footer">
                <el-button @click="emailVisible = false">取 消</el-button>
                <el-button type="primary" @click="handleConfirm('ruleForm3',form)">确 定</el-button>
              </span>
            </el-dialog>
          </el-tabs>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
import { getUserInfo, getUserInfoDetail, changePassWord } from "@/api/ajax";
import axios from "axios";
export default {
  name: "editPersonal",
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm2.checkPass !== "") {
          this.$refs.ruleForm2.validateField("checkPass");
        } else {
          var pattern = /^.*(?=.{6,})(?=.*\d)(?=.*[A-Z])(?=.*[a-z]).*$/;
          if (!pattern.test(value)) {
            callback(new Error("密码应包含数字、大小写字母、特殊字符"));
          }
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm2.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      message: "demo",
      ruleForm2: {
        pass: "",
        checkPass: "",
        prepass: ""
      },
      rules2: {
        pass: [{ required: true, validator: validatePass, trigger: "blur" }],
        checkPass: [
          { required: true, validator: validatePass2, trigger: "blur" }
        ],
        prepass: [
          { required: true, message: "请输入原密码", trigger: "change" }
        ]
      },
      url: "upload/a/h/api/common/upload",
      imageUrl: "",
      headers: {},
      dialogVisible: false,
      emailVisible: false,
      tabPosition: "left",
      coltitle: "基本资料（*为必填项）",
      form: {
        name: "",
        radio: "1",
        degree: {
          selected: "",
          options: [
            {
              value: "30",
              label: "博士"
            },
            {
              value: "40",
              label: "硕士"
            },
            {
              value: "50",
              label: "学士"
            },
            {
              value: "60",
              label: "高中"
            },
            {
              value: "70",
              label: "初中"
            },
            {
              value: "6",
              label: "无学历"
            }
          ]
        },
        major: {
          selected: "",
          options: [
            {
              value: "互联网IT",
              label: "互联网IT"
            },
            {
              value: "金融",
              label: "金融"
            },
            {
              value: "房地产/建筑",
              label: "房地产/建筑"
            },
            {
              value: "贸易/零售/物流",
              label: "贸易/零售/物流"
            },
            {
              value: "教育/传媒/广告",
              label: "教育/传媒/广告"
            },
            {
              value: "服务业",
              label: "服务业"
            },
            {
              value: "市场/销售",
              label: "市场/销售"
            },
            {
              value: "人事/财务/销售",
              label: "人事/财务/销售"
            }
          ]
        },
        edit: false,
        pwd: {
          value: "",
          hidevalue: "*********",
          hide: true
        },
        email: {
          value: "",
          hidevalue: "*******",
          hide: true
        }
      },
      rules: {
        name: [
          { required: true, message: "请输入昵称", trigger: "blur" },
          {
            min: 1,
            max: 25,
            message: "昵称输入过长，请重新输入",
            trigger: "blur"
          }
        ]
      }
    };
  },
  methods: {
    getUserinfo: function() {
      getUserInfoDetail().then(res => {
        if (localStorage.getItem("token")) {
          this.headers.token = localStorage.getItem("token");
        }

        if (res.data.data.nickName) {
          this.form.name = res.data.data.nickName;
        }
        if (res.data.data.gender) {
          this.form.radio = res.data.data.gender.toString();
        }
        if (res.data.data.diplomas) {
          this.form.degree.selected = res.data.data.diplomas.toString();
        }
        if (res.data.data.occupation) {
          this.form.major.selected = res.data.data.occupation.toString();
        }
        if (res.data.data.email) {
          this.form.email.value = res.data.data.email;
        }
        if (res.data.data.avatar) {
          this.imageUrl = res.data.data.avatar;
        }
      });
    },
    handleConfirm(formName, form) {
      if (formName == "ruleForm3") {
         this.$alert(`已发送验证邮件至${form.email.value},<div>请登录邮箱进行验证</div>`, '确认修改', {
          dangerouslyUseHTMLString: true
        }).then(()=>{
          this.emailVisible=false
          
        },()=>{ this.emailVisible=false});
      } else {
        this.$refs[formName].validate(valid => {
          if (valid) {
            console.log(form);
            let params = {};
            params = {};
            params.newPwd = form.pass;
            params.oldPwd = form.prepass;
            axios({
              method: "put",
              data: params,
              url: "account/a/h/api/user/pwd",
              headers: {
                "Content-Type": "application/json",
                token: localStorage.getItem("token")
              }
            }).then(res => {
              if (res.data.code != 0) {
                this.$message.error(res.data.msg);
                this.dialogVisible = false;
              } else {
                this.$message({
                  message: "更新成功",
                  type: "success"
                });
                _(form).forEach((v, i, arr) => {
                  form[i] = "";
                });
                this.dialogVisible = false;
              }
            });
          }
        });
      }
    },
    submitForm: function(ruleForm, form) {
      this.$refs[ruleForm].validate(valid => {
        if (valid) {
          let params = {};
          if (this.imageUrl) {
            params.avatar = this.imageUrl;
          }
          params.diplomas = this.form.degree.selected;
          params.email = this.form.email.value;
          params.gender = this.form.radio;
          params.nickName = this.form.name;
          params.occupation = this.form.major.selected;
          axios({
            method: "put",
            data: params,
            url: "account/a/h/api/user/baseinfo",
            headers: {
              "Content-Type": "application/json",
              token: localStorage.getItem("token")
            }
          }).then(res => {
            if (res.data.code != 0) {
              this.$message.error(res.data.msg);
            } else {
              this.$message({
                message: "更新成功",
                type: "success"
              });
            }
          });
        }
      });
    },
    beforeAvatarUpload: function() {},
    handleAvatarSuccess: function(res) {
      this.imageUrl = res.data;
      let params = {};
      params.diplomas = this.form.degree.selected;
      params.email = this.form.email.value;
      params.gender = this.form.radio;
      params.nickName = this.form.name;
      params.occupation = this.form.major.selected;
      params.avatar = this.imageUrl;
      axios({
        method: "put",
        data: params,
        url: "account/a/h/api/user/baseinfo",
        headers: {
          "Content-Type": "application/json",
          token: localStorage.getItem("token")
        }
      }).then(res => {
        if (res.data.code != 0) {
          this.$message.error(res.data.msg);
        } else {
          this.$message({
            message: "头像更新成功",
            type: "success"
          });
        }
      });
    },
    handleClick: function(tab) {
      switch (tab.label) {
        case "基础资料":
          this.coltitle = "基本资料（*为必填项）";

          break;
        case "头像设置":
          this.coltitle = "头像设置";

          break;
        case "账号信息":
          this.coltitle = "账号信息";
          break;
      }
    },
    edit: function(item) {
      item.edit = !item.edit;
    },
    openpwd() {},
    openemail() {}
  },
  components: {},
  mounted() {
    this.getUserinfo();
  }
};
</script>

<style lang="less" scoped>
@import "~styles/z.less";
@import "~styles/css3.less";
@import "~styles/variables.less";
.wrapper {
  .personal_info {
    .MainFont;
    font-weight: bold;
  }
  .upload {
    display: flex;
    width: 30%;
    justify-content: space-between;
    .userInfo {
      display: flex;
      .rel;
      flex-direction: column;
      justify-content: space-around;
      .slash {
        .slash;
        .abs;
        top: 137px;
        left: 78px;
      }
    }
    .avatar-uploader {
      .rel;
      .tip {
        .abs;
        .dib;
        z-index: 10;
        bottom: 0;
        right: 0;
        height: 30px;
        line-height: 30px;
        width: 90px;
        background: rgba(242, 242, 242, 0.639215);
      }
      /deep/ .el-upload {
        border: 1px dashed #8c939d;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
        &:hover {
          border-color: #409eff;
        }
      }
    }
  }
  .header-title {
    margin: 20px 0;
    font-weight: bold;
    border-bottom: 1px solid #ccc;
    padding: 10px;
    font-size: 20px;
  }
  .editinfo {
    color: #409eff;
    cursor: pointer;
    padding: 10px;
  }
  .seefont {
    font-size: 20px;
    display: inline-block;
    color: #409eff;
    cursor: pointer;
  }
  .editfont {
    color: #409eff;
    cursor: pointer;
    font-size: 15px;
    display: inline-block;
    padding: 0 10px;
  }
}
</style>
